package com.litelimit.core.service.loader;

import com.litelimit.core.service.log.LiteLimitLog;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;

@Data
@RequiredArgsConstructor
public class LuaScriptLoader {

    private final StringRedisTemplate stringRedisTemplate;

    private final DefaultRedisScript<Boolean> script = new DefaultRedisScript<>();

    @PostConstruct
    public void init(){
        try {
            ClassPathResource resource = new ClassPathResource("lua/fixedWindow.lua");
            script.setScriptSource(new ResourceScriptSource(resource));
            script.setResultType(Boolean.class);
        } catch (Exception e) {
            LiteLimitLog.log.error("初始化lua脚本失败");
        }
    }
}
